//
//  HomeSascanVC.m
//  Ginix
//
//  Created by 冯志强 on 2025/3/4.
//

#import "HomeSascanVC.h"
#import "SGQRCode.h"

@interface HomeSascanVC ()<SGScanCodeDelegate>
{
    SGScanCode *scanCode;
}
@property (nonatomic, strong) SGScanView *scanView;
@property (nonatomic, strong) UILabel *promptLabel;
@end

@implementation HomeSascanVC

- (void)dealloc {
    NSLog(@"XCQRCodeVC - dealloc");
    
    [self stop];
}

- (void)start {
    [scanCode startRunning];
    [self.scanView startScanning];
}

- (void)stop {
    [scanCode stopRunning];
    [self.scanView stopScanning];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"扫码激活设备";
    self.navgationView.infoLabel.textColor = RDRGB(0x714933);
    self.bgView.hidden = YES;
    self.view.backgroundColor = [UIColor blackColor];

    [self configureUI];
    
    [self configureQRCode];
}

- (void)configureUI {
    [self.view addSubview:self.scanView];
    [self.view addSubview:self.promptLabel];
}

- (void)configureQRCode {
    scanCode = [SGScanCode scanCode];
    scanCode.preview = self.view;
    scanCode.delegate = self;
    CGFloat w = 0.7;
    CGFloat x = 0.5 * (1 - w);
    CGFloat h = (self.view.frame.size.width / self.view.frame.size.height) * w;
    CGFloat y = 0.5 * (1 - h);
    /// 扫描范围。对应辅助扫描框的frame（borderFrame）设置
    scanCode.rectOfInterest = CGRectMake(y, x, h, w);
    [scanCode startRunning];
}

- (void)scanCode:(SGScanCode *)scanCode result:(NSString *)result {
    [self stop];
    [scanCode playSoundEffect:@"scan_end_sound.caf"];
    if (self.delegate && [_delegate respondsToSelector:@selector(requestCodeWith:)]) {
        [_delegate requestCodeWith:result];
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (SGScanView *)scanView {
    if (!_scanView) {
        SGScanViewConfigure *configure = [[SGScanViewConfigure alloc] init];
        configure.cornerLocation = SGCornerLoactionInside;
        configure.cornerWidth = 1;
        configure.cornerLength = 25;
        configure.isShowBorder = YES;
        configure.scanlineStep = 2;
        configure.scanline = @"scan_scanline";
        configure.autoreverses = YES;

        CGFloat x = 0;
        CGFloat y = 0;
        CGFloat w = self.view.frame.size.width;
        CGFloat h = self.view.frame.size.height;
        _scanView = [[SGScanView alloc] initWithFrame:CGRectMake(x, y, w, h) configure:configure];
        [_scanView startScanning];
    }
    return _scanView;
}

- (UILabel *)promptLabel {
    if (!_promptLabel) {
        _promptLabel = [[UILabel alloc] init];
        _promptLabel.backgroundColor = [UIColor clearColor];
        CGFloat promptLabelX = 0;
        CGFloat promptLabelY = 0.73 * self.view.frame.size.height;
        CGFloat promptLabelW = self.view.frame.size.width;
        CGFloat promptLabelH = 25;
        _promptLabel.frame = CGRectMake(promptLabelX, promptLabelY, promptLabelW, promptLabelH);
        _promptLabel.textAlignment = NSTextAlignmentCenter;
        _promptLabel.font = [UIFont boldSystemFontOfSize:13.0];
        _promptLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6];
        _promptLabel.text = @"将二维码/条码放入框内, 即可自动扫描";
    }
    return _promptLabel;
}

@end
